$(function () {
    bind();
    bind2();
});
var example,example2;
function bind() {
    example=$('#example').DataTable({
        "processing": true, //开启读取服务器数据时显示正在加载中……特别是大数据量的时候，开启此功能比较好
        "serverSide": true, //开启服务器模式，使用服务器端处理配置datatable。
        "pagingType": "full_numbers", //详细分页组，可以支持直接跳转到某页
        "paging": true,      //显示翻页
        "bSort": false,
        "scrollX": true,
        "info": true,
        "destroy": true,
        "searching": false,// 搜索栏
        "retrieve": false,
        "scrollY": true,
        "autoWidth": true,    //自动宽度true容易造成header的actions按钮队不齐，且搜索redraw后，表格跳动
        "columns": [
            {"data": "id", "title": "id"},
            {"data": "province", "title": "省"},
            {"data": "city", "title": "市"},
            {"data": "hospitalName", "title": "医院名称"},
            {"data": "address", "title": "地址"},
            {"data": "postOfficeCode", "title": "邮政编码"},
            {"data": "phoneNumber", "title": "电话"},
            {"data": "deanName", "title": "院长"},
            {"data": "bedNum", "title": "床位数"},
            {"data": "level", "title": "等级"},
            {"data": "saleName", "title": "销售"},
            {"data": "operatorOne", "title": "代理商一"},
            {"data": "operatorTwo", "title": "代理商二"},
            {"data": "operatorThree", "title": "代理商三"},
            {"data": "operatorFour", "title": "代理商四"},
            {"data": "directorName", "title": "主任姓名"},
            {"data": "currentUserProduct", "title": "目前医院使用产品"},
            {"data": "carryBill", "title": "提单"},
            {"data": "tender", "title": "招标"},
            {"data": "thingsPrice", "title": "物价"},
            {"data": "repayment", "title": "回款"},
            {"data": "installInto", "title": "主机装机进院"},
            {"data": "cycleUse", "title": "耗材循环使用"},
            {"data": "note", "title": "备注"}
        ],
        "deferRender": true,
        "ajax": function (data, callback, settings) {//向后传值得方式，这里使用post，默认为get，
            data.hospitalName=$("#hospitalNameQuery").val();
            $.ajax({
                type: "post",
                data: JSON.stringify(data),
                url: "/hospital/queryList",
                contentType: "application/json",
                dataType: "json",
                async: true,
                success: function (data) {
                    callback(data);
                },
                error: function (data) {

                }
            });
        },
        "createdRow": function (row, datax, dataIndex) {
            for (var index = 0; index < row.childElementCount; index++) {
                var $td = $('td', $(row)).eq(index);
                $td.html("<a style='cursor:pointer'>" + $td.text() + "</a>");
            }
            var $td = $('td', $(row)).eq(1);
            $td.parent().click(function () {
                $("#hospitalForm").json2form({data: datax});
                $("#hospitalInfo").modal('show');
            });

        },
        "drawCallback": function (settings) {
        },
        "footerCallback": function (tfoot, data, start, end, display) {
        },
        "initComplete": function (settings, json) {
            $("#example th").css("text-align", "center").css("font-weight", "bold");
            $("#example td").css("text-align", "center");
        }
    });
}

function updateOperatorInfo() {

    var data = {

        "id": $("#hosId").val(),
        "saleName": $("#saleName").val(),
        "operatorOne": $("#operatorOne").val(),
        "operatorTwo": $("#operatorTwo").val(),
        "operatorThree": $("#operatorThree").val(),
        "operatorFour": $("#operatorFour").val(),
        "directorName": $("#directorName").val(),
        "currentUserProduct": $("#currentUserProduct").val(),
        "carryBill": $("#carryBill").val(),
        "tender": $("#tender").val(),
        "thingsPrice": $("#thingsPrice").val(),
        "repayment": $("#repayment").val(),
        "installInto": $("#installInto").val(),
        "cycleUse": $("#cycleUse").val(),
        "note": $("#note").val()
    };
    $.ajax({
        type: "post",
        data: JSON.stringify(data),
        url: "/hospital/update",
        contentType: "application/json",
        dataType: "json",
        async: true,
        success: function (data) {
            alert("修改成功")
        },
        error: function (data) {
        }
    });
}


function bind2() {
    example2= $('#example2').DataTable({
        "processing": true, //开启读取服务器数据时显示正在加载中……特别是大数据量的时候，开启此功能比较好
        "serverSide": true, //开启服务器模式，使用服务器端处理配置datatable。
        "pagingType": "full_numbers", //详细分页组，可以支持直接跳转到某页
        "paging": true,      //显示翻页
        "bSort": false,
        "scrollX": true,
        "info": true,
        "destroy": true,
        "searching": false,// 搜索栏
        "retrieve": false,
        "scrollY": true,
        "autoWidth": true,    //自动宽度true容易造成header的actions按钮队不齐，且搜索redraw后，表格跳动
        "columns": [
            {"data": "id", "title": "id"},
            {"data": "createTime", "title": "日期"},
            {"data": "province", "title": "省"},
            {"data": "city", "title": "市"},
            {"data": "hospitalName", "title": "医院名称"},
            {"data": "hospitalType", "title": "医院类别"},
            {"data": "department", "title": "科室"},
            {"data": "director", "title": "行政主任"},
            {"data": "directorPhone", "title": "行政主任联系方式"},
            {"data": "dealName", "title": "经销商名称"},
            {"data": "dealerControlName", "title": "经销商实际控制人"},
            {"data": "dealerControlPhone", "title": "经销商实际控制人联系方式"},
            {"data": "dealerOperatorName", "title": "经销商实际操作人"},
            {"data": "dealerOperatorPhone", "title": "经销商实际操作人电话"},
            {"data": "saleProduct", "title": "经销商实际操作人当前在科室销售产品"},
            {"data": "visitPlan", "title": "拜访计划"},
            {"data": "visitResult", "title": "拜访结果"},
            {"data": "actionPlan", "title": "行动计划"},
            {"data": "userName", "title": "创建人姓名"}

        ],
        "deferRender": true,
        "ajax": function (data, callback, settings) {//向后传值得方式，这里使用post，默认为get，
            data.hospitalName=$("#hospitalNameAdd").val();
            $.ajax({
                type: "post",
                data: JSON.stringify(data),
                url: "/hospitalVisit/queryList",
                contentType: "application/json",
                dataType: "json",
                async: true,
                success: function (data) {
                    callback(data);
                },
                error: function (data) {

                }
            });
        },
        "createdRow": function (row, datax, dataIndex) {
            for (var index = 0; index < row.childElementCount; index++) {
                var $td = $('td', $(row)).eq(index);
                $td.html("<a style='cursor:pointer'>" + $td.text() + "</a>");
            }
            var $td = $('td', $(row)).eq(1);
            $td.parent().click(function () {
                $("#hospitalVisitForm").json2form({data: datax});
                $("#hospitalVisitInfo").modal('show');
            });

        },
        "drawCallback": function (settings) {
        },
        "footerCallback": function (tfoot, data, start, end, display) {
        },
        "initComplete": function (settings, json) {
            $("#example2 th").css("text-align", "center").css("font-weight", "bold");
            $("#example2 td").css("text-align", "center");
        }
    });
}



function updateHospitalVisit() {
    var data = {

        "id": $("#visitId").val(),
        "province": $("#province2").val(),
        "city": $("#city2").val(),
        "hospitalName": $("#hospitalName2").val(),
        "hospitalType": $("#hospitalType").val(),
        "department": $("#department").val(),
        "director": $("#director").val(),
        "directorPhone": $("#directorPhone").val(),
        "dealName": $("#dealName").val(),
        "dealerControlName": $("#dealerControlName").val(),
        "dealerControlPhone": $("#dealerControlPhone").val(),
        "dealerOperatorName": $("#dealerOperatorName").val(),
        "dealerOperatorPhone": $("#dealerOperatorPhone").val(),
        "saleProduct": $("#saleProduct").val(),
        "visitPlan": $("#visitPlan").val(),
        "visitResult": $("#visitResult").val(),
        "actionPlan": $("#actionPlan").val()
    };
    $.ajax({
        type: "post",
        data: JSON.stringify(data),
        url: "/hospitalVisit/update",
        contentType: "application/json",
        dataType: "json",
        async: true,
        success: function (data) {
            alert("修改成功")
        },
        error: function (data) {
        }
    });
}

function saveHospitalVisitInfo() {
    var data = {
        "province": $("#province3").val(),
        "city": $("#city3").val(),
        "hospitalName": $("#hospitalName3").val(),
        "hospitalType": $("#hospitalType3").val(),
        "department": $("#department3").val(),
        "director": $("#director3").val(),
        "directorPhone": $("#directorPhone3").val(),
        "dealName": $("#dealName3").val(),
        "dealerControlName": $("#dealerControlName3").val(),
        "dealerControlPhone": $("#dealerControlPhone3").val(),
        "dealerOperatorName": $("#dealerOperatorName3").val(),
        "dealerOperatorPhone": $("#dealerOperatorPhone3").val(),
        "saleProduct": $("#saleProduct3").val(),
        "visitPlan": $("#visitPlan3").val(),
        "visitResult": $("#visitResult3").val(),
        "actionPlan": $("#actionPlan3").val()
    };
    $.ajax({
        type: "post",
        data: JSON.stringify(data),
        url: "/hospitalVisit/insert",
        contentType: "application/json",
        dataType: "json",
        async: true,
        success: function (data) {
            alert("添加成功")
        },
        error: function (data) {
        }
    });
}

function queryVisit() {
    example2.draw();
}

function queryHospital() {
    example.draw();
}
function exportHospitalData() {
    $("#hospitalExport").attr("action","/hospital/export");
    $("#hospitalExport").submit();
}

function exportHospitalVisitData() {
    $("#hospitalVisitExport").attr("action","/hospitalVisit/export");
    $("#hospitalVisitExport").submit();
}

function addHospitalInfo() {
    var data = {
        "province": $("#province4").val(),
        "city": $("#city4").val(),
        "hospitalName": $("#hospitalName4").val(),
        "address": $("#address4").val(),
        "postOfficeCode": $("#postOfficeCode4").val(),
        "phoneNumber": $("#phoneNumber4").val(),
        "deanName": $("#deanName4").val(),
        "bedNum": $("#bedNum4").val(),
        "level": $("#level4").val(),
        "operatorOne": $("#operatorOne4").val(),
        "operatorTwo": $("#operatorTwo4").val(),
        "operatorThree": $("#operatorThree4").val(),
        "operatorFour": $("#operatorFour4").val(),
        "directorName": $("#directorName4").val(),
        "currentUserProduct": $("#currentUserProduct4").val(),
        "carryBill": $("#carryBill4").val(),
        "tender": $("#tender4").val(),
        "thingsPrice": $("#thingsPrice4").val(),
        "repayment": $("#repayment4").val(),
        "installInto": $("#installInto4").val(),
        "cycleUse": $("#cycleUse4").val(),
        "note": $("#note4").val()
    };
    $.ajax({
        type: "post",
        data: JSON.stringify(data),
        url: "/hospital/insert",
        contentType: "application/json",
        dataType: "json",
        async: true,
        success: function (data) {
            alert("添加成功")
        },
        error: function (data) {
        }
    });
}

